package sg.edu.nus.iss.phoenix.schedule.delegate;
/**
 * @author Sanghrajka Vipul - A0092656W
 *
 */
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import sg.edu.nus.iss.phoenix.authenticate.entity.User;
import sg.edu.nus.iss.phoenix.dao.DAOFactory;
import sg.edu.nus.iss.phoenix.dao.DBConnection;
import sg.edu.nus.iss.phoenix.schedule.dao.AnnualScheduleDAO;
import sg.edu.nus.iss.phoenix.schedule.dao.ProgramSlotDAO;
import sg.edu.nus.iss.phoenix.schedule.dao.WeeklyScheduleDAO;
import sg.edu.nus.iss.phoenix.schedule.entity.AnnualSchedule;
import sg.edu.nus.iss.phoenix.schedule.entity.ProgramSlot;
import sg.edu.nus.iss.phoenix.schedule.entity.WeeklySchedule;
import sg.edu.nus.iss.phoenix.schedule.service.MaintainScheduleService;

public class MaintainScheduleDelegate {
private MaintainScheduleService maintainScheduleService;

/**
 * this is the constructor to initialize the MaintainScheduleService object.
 */
	public MaintainScheduleDelegate(){
		maintainScheduleService = new MaintainScheduleService();
	}
	
	/**
	 * this method is used to save the weekly schedule
	 * @param ws
	 * @throws Exception
	 */
	public void saveWeeklySchedule(WeeklySchedule ws) throws Exception{
		try{
			long ws_id = maintainScheduleService.getDaoFactory().getWeeklyScheduleDAO().createWeeklySchedule(ws);
			ws.setId(ws_id);
			maintainScheduleService.getDaoFactory().getProgramSlotDAO().saveProgramSlot(ws);
		}
		catch(Exception e){
			e.printStackTrace();
			throw new Exception (e.getMessage());
		}

	}
	
/**
 * this method is used to return the weekly schedule based on the week and the year attributes of the weeklyschdule.
 * @param ws
 * @return
 * @throws SQLException
 */
	public WeeklySchedule findWeeklySchedule(WeeklySchedule ws) throws SQLException{
		return maintainScheduleService.getWeeklyScheduleDAO().searchMatching(ws.getYear(), ws.getWeekNumber());
	}
	
	/**
	 * This method used to return Annual Schedule based on the annual schedule id- year.
	 * @param annualScheduleId
	 * @return
	 * @throws SQLException
	 */
	public AnnualSchedule findAnnualSchedule(int annualScheduleId) throws SQLException{
		System.out.println(annualScheduleId);
		return maintainScheduleService.getAnnualScheduleDAO().searchMatching(annualScheduleId);
		

	}
	
	/**
	 * This method is used to create a annual schedule 
	 * @param annualSchedule
	 * @param user
	 * @throws SQLException
	 */
	public void createAnnualSchedule(AnnualSchedule annualSchedule, User user) throws SQLException{
		 maintainScheduleService.getAnnualScheduleDAO().createAnnualSchedule(annualSchedule.getYear(), user.getId());
		

	}

	/**
	 * This method will delete the weekly schedule and all the program slots associated with it.
	 * @param ws
	 * @throws SQLException
	 */
	public void deleteWeeklySchedule(WeeklySchedule ws) throws SQLException {
		ws = maintainScheduleService.getWeeklyScheduleDAO().searchMatching(ws.getYear(), ws.getWeekNumber());
		
		deleteProgramSlots(ws);
		maintainScheduleService.getWeeklyScheduleDAO().deleteWeeklySchedule(ws.getId());
		
	}

	/**
	 * This methos will delete all the programs slots from a weekly schedule
	 * @param ws
	 * @throws SQLException
	 */
	public void deleteProgramSlots(WeeklySchedule ws) throws SQLException {
		deleteWeeklySchedule(ws);
		
	}
	
}
